﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CROSLAND.BL.BE;
using System.Data.Common;
using System.Data;

namespace CROSLAND.DL.DALC.MSSQL
{
    public class MaterialDALC : BaseDALC, IMaterialDALC
    {
        
        #region IMaterialDALC Members

        public List<MaterialBE> buscarMateriales(string pCodAnterior, string pNombre,
            string pJerarquia, string pOrgVtas, string pCanalDist, string usuarioWeb)
        {
            throw new NotImplementedException();
        }

        public MaterialBE leerMaterial(string pCodigo, string pOrgVtas, string pCanalDist, string usuarioWeb)
        {
            throw new NotImplementedException();          
        }

        public List<GenericBE> buscarGrupos(string pCodGrupo)
        {
            List<GenericBE> entitys = new List<GenericBE>();

            DbCommand command = dbProvider.GetStoredProcCommand("ZWEB_LISTA_GRPMAT");
            dbProvider.AddInParameter(command, "IM_MVGR1", DbType.String, pCodGrupo);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    GenericBE entity = new GenericBE();

                    entity.Codigo = dr["MVGR1"].ToString();
                    entity.Descripcion = dr["BEZEI"].ToString();

                    entitys.Add(entity);
                }
            }

            return entitys;
        }

        #endregion

        //WEBDIST
        public MaterialBE leerMaterial(string pCodigo, string pOrgVtas, string pCanalDist)
        {
            return leerMaterial(pCodigo, pOrgVtas, pCanalDist, "");
        }

        #region IMaterialDALC Members

        /// <summary>
        /// No usar
        /// </summary>
        /// <param name="pCod"></param>
        /// <param name="pCodAnt"></param>
        /// <param name="pDesc"></param>
        /// <param name="pJerarquia"></param>
        /// <param name="pOrgVtas"></param>
        /// <param name="pCanalDist"></param>
        /// <param name="usuarioWeb"></param>
        /// <returns></returns>
        public List<MaterialBE> buscarMateriales(string pCod, string pCodAnt, string pDesc, string pJerarquia, string pOrgVtas, string pCanalDist, string usuarioWeb)
        {
            throw new NotImplementedException();
        }

        #endregion
        
        #region Miembros de IMaterialDALC
        
        public List<MaterialBE> buscarMateriales(string pCodMat, string pOrgVtas, string pCanalDist, string usuarioWeb, string sector)
        {
            throw new NotImplementedException();
        }

        #endregion


        public List<MaterialBE> buscarMateriales(string pCodMat, string pDesMat, string pJerMat, string pOrgVtas, string pCanalDist, string ofVtas, string usuarioWeb, string sector, string centro, string almacen)
        {
            List<MaterialBE> entitys = new List<MaterialBE>();
            DbCommand command = dbProvider.GetStoredProcCommand("USPR_BUSQUEDA_MATERIAL");

            dbProvider.AddInParameter(command, "MATNR", DbType.String, pCodMat.Trim() == "" ? "" : pCodMat);
            dbProvider.AddInParameter(command, "BISMT", DbType.String, "");
            dbProvider.AddInParameter(command, "MAXTX", DbType.String, pDesMat);
            dbProvider.AddInParameter(command, "PRDHA", DbType.String, pJerMat.Trim());
            dbProvider.AddInParameter(command, "VKORG", DbType.String, pOrgVtas);
            dbProvider.AddInParameter(command, "VTWEG", DbType.String, pCanalDist);
            dbProvider.AddInParameter(command, "SPART", DbType.String, sector);
            dbProvider.AddInParameter(command, "VKBUR", DbType.String, ofVtas);
            dbProvider.AddInParameter(command, "WERKS", DbType.String, centro);
            dbProvider.AddInParameter(command, "LGORT", DbType.String, almacen);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    MaterialBE objMaterial = new MaterialBE();
                    objMaterial.CodMaterial = dr["MATNR"].ToString();
                    objMaterial.DescMaterial = dr["MAKTX"].ToString();
                    objMaterial.CodUMedida = dr["MEINS"].ToString();
                    objMaterial.PrecioUnit = Convert.ToDecimal(dr["NETPR"].ToString());
                    objMaterial.Plista = Convert.ToDecimal(dr["NETPR"].ToString());
                    objMaterial.Ppromo = Convert.ToDecimal(dr["PRPROM"].ToString());
                    objMaterial.marca = dr["MARCA"].ToString();
                    objMaterial.modelo = dr["MODELO"].ToString();
                    objMaterial.moneda = dr["MONEDA"].ToString();
                    objMaterial.Stock = Convert.ToDecimal(dr["STOCK"].ToString());
                    objMaterial.materialIGV = dr["IGV"].ToString();
                    objMaterial.materialISC = dr["ISC"].ToString();
                    entitys.Add(objMaterial);
                }
            }

            return entitys;
        }

        public List<MaterialBE> buscarMaterialesReemplazo(string pCodMat)
        {
            List<MaterialBE> lstMat = new List<MaterialBE>();
            DbCommand command = dbProvider.GetStoredProcCommand("USPR_BUSQUEDA_MAT_REEMPLAZO");
            dbProvider.AddInParameter(command, "P_MATNR", DbType.String, pCodMat);
            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    MaterialBE objMaterial = new MaterialBE();
                    objMaterial.CodMaterial = dr["MATNR"].ToString();
                    objMaterial.DescMaterial = dr["MAKTX"].ToString();
                    objMaterial.CodUMedida = dr["MEINS"].ToString();
                    objMaterial.PrecioUnit = Convert.ToDecimal(dr["NETPR"].ToString());
                    objMaterial.Plista = Convert.ToDecimal(dr["NETPR"].ToString());
                    objMaterial.Ppromo = Convert.ToDecimal(dr["PRPROM"].ToString());
                    objMaterial.marca = dr["MARCA"].ToString();
                    objMaterial.modelo = dr["MODELO"].ToString();
                    objMaterial.Stock = Convert.ToDecimal(dr["STOCK"].ToString());
                    lstMat.Add(objMaterial);
                }
            }

            return lstMat;
        }

        public List<GenericBE> caracteristicaMat(string pCodMat, string pLote)
        {
            List<GenericBE> lstMat = new List<GenericBE>();
            GenericBE objGenBE = null;
            DbCommand command = dbProvider.GetStoredProcCommand("USPR_LEE_VEHICULO_CARACTERISTICA");
            dbProvider.AddInParameter(command, "P_MATNR", DbType.String, pCodMat);
            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    objGenBE = new GenericBE();
                    objGenBE.Codigo = dr["CARACTERISTICA"].ToString();
                    objGenBE.Descripcion = dr["VALOR"].ToString();
                    lstMat.Add(objGenBE);
                }
            }

            return lstMat;
        }
    }
}
